#-------------------------------------------------------------------------------
# Name:        Sprite
# Purpose:
#
# Author:      Michael Blackwood
#
# Created:     23/10/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, logging
import base
from ...tools import subpixelsurface

class Sprite(base.Base):


  def __init__(self, objtype, name, attributes, imagehandler):
    base.Base.__init__(self, objtype, name, attributes, imagehandler)
    self.ancestors.append('sprite')
    self.attributes['initialframe'] = int(self.attributes['initialframe'])
    self.attributes['frame'] = self.attributes['initialframe']
    self.attributes['solid'] = self.attributes['solid'] == 'True'

    self.getSpriteFrames()
    self.generateRects()

  def getSpriteFrames(self):
    """ Break up the sprite sheet into its frames. """
    size = self.imagehandler.getSprite(self.attributes['sprite']).get_size()
    framesize = (size[0] / 4, size[1] / 5)
    self.attributes['framesize'] = framesize
    self.attributes['frames'] = []

    for j in range(0, 5):
      for i in range(0, 4):
        self.attributes['frames'].append((i * framesize[0], j * framesize[1], framesize[0], framesize[1]))


  def generateRects(self):

    """ Generate and store a list of rects for each from of the sprite. """

    self.attributes['rects'] = []
    for frame in range(0, 20):
      tempsurf = pygame.Surface(self.attributes['framesize']).convert_alpha()
      tempsurf.fill((0,0,0,0))
      sprite = self.imagehandler.getSprite(self.attributes['sprite'])
      frames = self.attributes['frames']
      tempsurf.blit(sprite, (0,0), frames[frame])
      self.attributes['rects'].append(tempsurf.get_bounding_rect(255))

  def getBoundingRect(self):

    """ Returns rect for collisions"""
    rect = self.getBoundingBox()
    pos = self.attributes['pos']
    rect.topleft = (rect.topleft[0] + pos[0], rect.topleft[1] + pos[1])

    return rect

  def getRect(self):

    return pygame.Rect(self.attributes['pos'], self.attributes['framesize'])

  def getBoundingBox(self):

    """ Returns bounding box of current frame's surface"""

    return self.attributes['rects'][self.attributes['frame']]

  def getBoundingBuffer(self):

    """ Returns a tuple containing the distance of each edge of the bounding box from the edge
        of the sprite rect. If that makes any sense at all, PROBABLY NOT.
        (left edge, right edge, top edge, bottom edge) """

    framesize = self.attributes['framesize']
    rect = self.getBoundingBox()

    return (rect.left, framesize[0] - rect.right, rect.top, framesize[1] - rect.bottom)


  def draw(self, surface, offset, common):

    """ Draw, yo. """

    if self.attributes['visable']:

      pos = self.attributes['pos']
      pos = (pos[0] + offset[0], pos[1] + offset[1])
      rect = pygame.rect.Rect(pos, self.attributes['framesize'])

      # only draw in on screen
      if surface.get_rect().colliderect(rect):

        sprite = self.imagehandler.getSprite(self.attributes['sprite'])
        frames = self.attributes['frames']
        frame = self.attributes['frame']

        dirtyrect = surface.blit(sprite, pos, frames[frame])
        return dirtyrect


